package cn.signalpha.modules.system.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.signalpha.common.cache.ConfigKeys;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.dto.ConfigDTO;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.modules.system.entity.Config;
import cn.signalpha.modules.system.service.IConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ijpay.wxpay.WxPayApiConfig;
import com.ijpay.wxpay.WxPayApiConfigKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/admin/config")
@Api(value = "配置管理", tags = "配置管理")
public class ConfigController extends BaseController {

	private final WxMpService wxMpService;
	private final WxMaService wxMaService;
	private final IConfigService configService;

	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入config")
	public R<IPage<Config>> list(@ApiIgnore @RequestParam Map<String, Object> param, Query query) {
		param.put("status_equal", "1");
		return R.data(configService.page(Condition.getPage(query), Condition.getQueryWrapper(param, Config.class)));
	}

	@GetMapping("/info")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入config")
	public R<Config> detail(Config config) {
		return R.data(configService.getOne(Condition.getQueryWrapper(config)));
	}

	@PostMapping("/submit")
	@ApiOperation(value = "提交", notes = "传入config")
	public R submit(@Valid @RequestBody Config configDTO) {
		if(configDTO.getConfigKey().equals(ConfigKeys.WX_MP)){
			wxMpService.removeConfigStorage(wxMpService.getWxMpConfigStorage().getAppId());
			ConfigDTO.WxMpDTO a = JsonUtil.parse(configDTO.getConfigValue(), ConfigDTO.WxMpDTO.class);
			WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
			configStorage.setAppId(a.getAppId());
			configStorage.setSecret(a.getSecret());
			configStorage.setToken(a.getToken());
			configStorage.setAesKey(a.getAesKey());
			wxMpService.addConfigStorage(a.getAppId(), configStorage);
		}else if(configDTO.getConfigKey().equals(ConfigKeys.WX_MA)){
			wxMaService.removeConfig(wxMaService.getWxMaConfig().getAppid());
			ConfigDTO.WxMaDTO a = JsonUtil.parse(configDTO.getConfigValue(), ConfigDTO.WxMaDTO.class);
			WxMaDefaultConfigImpl configStorage = new WxMaDefaultConfigImpl();
			configStorage.setAppid(a.getAppId());
			configStorage.setSecret(a.getSecret());
			configStorage.setToken(a.getToken());
			configStorage.setAesKey(a.getAesKey());
			wxMaService.addConfig(a.getAppId(), configStorage);
		}else if(configDTO.getConfigKey().equals(ConfigKeys.WX_PAY)){
			ConfigDTO.WxPayDTO wxPayDTO = JsonUtil.parse(configDTO.getConfigValue(), ConfigDTO.WxPayDTO.class);
			WxPayApiConfig wxPayApiConfig = WxPayApiConfig.builder()
				.appId(wxPayDTO.getAppId())
				.mchId(wxPayDTO.getMchId())
				.apiKey(wxPayDTO.getApiKey2())
				.apiKey3(wxPayDTO.getApiKey3())
				.certPath(wxPayDTO.getCertPath())
				.keyPath(wxPayDTO.getKeyPath())
				.certP12Path(wxPayDTO.getCertP12Path())
				.platformCertPath(wxPayDTO.getPlatformCertPath())
				.exParams(wxPayDTO.getSerialNo())
				.build();
			WxPayApiConfigKit.removeThreadLocalAppId();
			WxPayApiConfigKit.putApiConfig(wxPayApiConfig);
		}
		configService.removeCache(configDTO.getConfigKey());
		return R.status(configService.saveOrUpdate(configDTO));
	}

}
